﻿using System.Collections.Generic;
using System.Text;
using System.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;

namespace BLL.SysManage
{
    public class T_PUB_MATERIAL
    {
        private readonly DALL.SysManage.T_PUB_MATERIAL dal = new DALL.SysManage.T_PUB_MATERIAL();
        public T_PUB_MATERIAL()
        { }

        /// <summary>
        /// 智能提示元素名称
        /// </summary>
        /// <returns></returns>
        public DataSet GetMaterialName()
        {
            return dal.GetMaterialName();
        }

        #region  Method
        /// <summary>
        /// 实体是否出库，采购，既不出库又不采购三类
        /// </summary>
        /// <param name="param1"></param>
        /// <param name="param2"></param>
        /// <returns></returns>
        public DataSet GetMaterialInfoByType(string param1, string param2)
        {
            string strWhere = " ";
            if ((param1 != "" || param1 != null) && (param2 != "" || param2 != null))
            {
                strWhere += string.Format(" and a.TYPE_ID in (select distinct (TYPE_ID) from T_PUB_MATERIALTYPE where FALG='{0}'or FALG = '{1}')", param1, param2);
            }
            if (param2 == "" || param2 == null)
            {
                strWhere += string.Format(" and a.TYPE_ID in (select distinct (TYPE_ID) from T_PUB_MATERIALTYPE where FALG='{0}')", param1);
            }
            return dal.GetMaterialInfo(strWhere);
        }
        /// <summary>
        /// 获取元素类型的状态为“无需采购并无需出入库-0”“需采购-1”“需采购并需出入库-2”
        /// </summary>
        /// <param name="MATERIAL_ID"></param>
        /// <returns></returns>
        public DataSet GetMaterialTypeState(int TYPE_ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select TYPE_ID,TYPE_NAME,FALG,ISUSED from T_PUB_MATERIALTYPE ");
            strSql.Append(" where TYPE_ID=" + TYPE_ID);
            strSql.Append(" and ISUSED='1'");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        /// <summary>
        /// 获取对应元素分类下的元素信息
        /// </summary>
        /// <param name="strTypeID">元素类型编号</param>
        /// <returns></returns>
        public DataSet GetMaterialInfo(string strTypeID, string strTypeName, string strIDS)
        {
            string strWhere = " ";
            if (!string.IsNullOrEmpty(strTypeID))
            {
                strWhere += string.Format(" and b.TYPE_ID in ({0})", strTypeID);
            }
            if (!string.IsNullOrEmpty(strTypeName))
            {
                strWhere += string.Format("and MATERIAL_NAME like '%{0}%'", strTypeName);
            }
            if (!string.IsNullOrEmpty(strIDS))
            {
                strWhere += string.Format(" and a.FLAG in ({0})", strIDS);
            }
            strWhere += " and a.ISUSED='1' ";
            return dal.GetMaterialInfo(strWhere);
        }
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int MATERIAL_ID)
        {
            return dal.Exists(MATERIAL_ID);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Model.SysManage.T_PUB_MATERIAL model)
        {
            return dal.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.SysManage.T_PUB_MATERIAL model)
        {
            return dal.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int MATERIAL_ID)
        {

            return dal.Delete(MATERIAL_ID);
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.SysManage.T_PUB_MATERIAL GetModel(int MATERIAL_ID)
        {

            return dal.GetModel(MATERIAL_ID);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Model.SysManage.T_PUB_MATERIAL> GetModelList(string strWhere)
        {
            return dal.GetListArray(strWhere);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return GetList("");
        }

        public List<Model.SysManage.T_PUB_MATERIAL> GetModelListByMaterialType(string strMaterialTypeID)
        {
            return GetModelList(" TYPE_ID= " + strMaterialTypeID);
        }
        #endregion  Method
    }
}
